﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 客户物资编码适配系统
{
    public partial class frmDataMainten : Form
    {
        UserInfo ui;
        SqlTools sqt;

        public frmDataMainten(UserInfo tmpui)
        {
            InitializeComponent();
            ui = tmpui;
            sqt = new SqlTools(GeneralTools.GetConnectionString("TPAMain"));
        }

        private void tp1_btnQuery_Click(object sender, EventArgs e)
        {
            string strDefaultSelectString = "select ID 序号,CustomerName 客户名称,MaterialID 客户物资代码,CustomerModel 客户型号规格,OwnModel 我公司型号规格,Ignore 忽略,Creater 创建者,CreateTime 创建时间,Modifier 修改者,ModifyTime 修改时间 from CustomerMaterialAdapt";
            if (!tp1_txtCustomerName .Text .Trim ().Equals (string.Empty ))
            {
                strDefaultSelectString += " where CustomerName like '%" + tp1_txtCustomerName.Text.Trim() + "%'";
            }
            if (!tp1_txtMaterialID.Text .Trim ().Equals (string.Empty ))
            {
                if (strDefaultSelectString.Contains("where"))
                {
                    strDefaultSelectString += " and ";
                }
                else
                {
                    strDefaultSelectString += " where ";
                }
                strDefaultSelectString += " MaterialID like '" + tp1_txtMaterialID.Text.Trim() + "'";
            }
            if (!tp1_txtModel.Text.Trim().Equals(string.Empty))
            {
                if (strDefaultSelectString.Contains("where"))
                {
                    strDefaultSelectString += " and ";
                }
                else
                {
                    strDefaultSelectString += " where ";
                }
                strDefaultSelectString += " OwnModel like '" + tp1_txtModel.Text.Trim() + "'";
            }
            tp1_dgv1.DataSource = sqt.GetData(strDefaultSelectString);
        }

        private void tsmiAutoSize_Click(object sender, EventArgs e)
        {
            tp1_dgv1.AutoResizeColumns();
        }

        private void tsmiDeleteSelected_Click(object sender, EventArgs e)
        {
            if (tp1_dgv1.SelectedRows.Count >0)
            {
                if (MessageBox.Show ("确定删除选中的"+tp1_dgv1 .SelectedRows.Count .ToString ()+"项吗？","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Question)==DialogResult.OK)
                {
                    string strDeleteString = "delete from CustomerMaterialAdapt";
                    for(int i=0;i<tp1_dgv1 .SelectedRows.Count;i++)
                    {
                        sqt.DoCmdExecuteNonQuery(strDeleteString + " where ID=" + GeneralTools.GetString(tp1_dgv1.SelectedRows[i].Cells["序号"].Value, "-1"));
                    }
                }
                tp1_btnQuery_Click(this, new EventArgs());
            }
        }

        private void tsmiIgnoreSelected_Click(object sender, EventArgs e)
        {
            if (tp1_dgv1.SelectedRows.Count > 0)
            {
                if (MessageBox.Show("确定忽略选中的" + tp1_dgv1.SelectedRows.Count.ToString() + "项吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    for (int i = 0; i < tp1_dgv1.SelectedRows.Count; i++)
                    {
                        sqt.DoCmdExecuteNonQuery("update CustomerMaterialAdapt set Ignore=1,Modifier='"+ui.UserName+ "'ModifyTime=GetDate(), where ID=" + GeneralTools.GetString(tp1_dgv1.SelectedRows[i].Cells["序号"].Value, "-1"));
                    }
                }
                tp1_btnQuery_Click(this, new EventArgs());
            }
        }

        private void tsmiUnignoreSelected_Click(object sender, EventArgs e)
        {
            if (tp1_dgv1.SelectedRows.Count > 0)
            {
                if (MessageBox.Show("确定取消忽略选中的" + tp1_dgv1.SelectedRows.Count.ToString() + "项吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    for (int i = 0; i < tp1_dgv1.SelectedRows.Count; i++)
                    {
                        sqt.DoCmdExecuteNonQuery("update CustomerMaterialAdapt set Ignore=0,Modifier='" + ui.UserName + "'ModifyTime=GetDate(), where ID=" + GeneralTools.GetString(tp1_dgv1.SelectedRows[i].Cells["序号"].Value, "-1"));
                    }
                }
                tp1_btnQuery_Click(this, new EventArgs());
            }
        }

        private void tp2_txtCustomerName_TextChanged(object sender, EventArgs e)
        {
            if((!tp2_txtCustomerName.Text .Trim ().Equals (string.Empty ))&& (!tp2_txtCustomerName.Text.Trim().Equals("%")))
            {
                sqt.FillCheckListBox(tp2_chklstCustomerNames, "select distinct ku002 from ku_table where (ku001 like '" + tp2_txtCustomerName.Text.Trim() + "%' or ku002 like '" + tp2_txtCustomerName.Text.Trim() + "%') order by ku002");
            }
        }

        private void tp2_btnSave_Click(object sender, EventArgs e)
        {
            tp2_dgv1.EndEdit();
            if (tp2_dgv1.Rows.Count ==0)
            {
                GeneralTools.ShowWarningMsg("没有可保存的数据！");
                return;
            }
            if (tp2_chklstCustomerNames .SelectedItems.Count ==0)
            {
                GeneralTools.ShowWarningMsg("请先设置要对应的客户名称！");
                tp2_chklstCustomerNames.Focus();
                return;
            }
            //检查数据完整性
            for(int i=0;i<tp2_dgv1 .Rows.Count;i++)
            {
                if (tp2_dgv1 .Rows [i].IsNewRow )
                {
                    continue;
                }
                //检查物资代码是否为空
                if (GeneralTools .GetString (tp2_dgv1 .Rows[i].Cells ["物资代码"].Value ,string.Empty ).Equals (string.Empty ))
                {
                    tp2_dgv1.Rows[i].Cells["状态"].Value = "物资代码不能为空！";
                    continue;
                }
                //检查我公司型号规格是否为空
                if (GeneralTools.GetString(tp2_dgv1.Rows[i].Cells["我公司型号规格"].Value, string.Empty).Equals(string.Empty))
                {
                    tp2_dgv1.Rows[i].Cells["状态"].Value = "我公司型号规格不能为空！";
                    continue;
                }
                //检查我公司型号规格是否存在
                if (sqt.RecordExist ("select ycl001 from ycl_table where ycl001 ='"+ GeneralTools.GetString(tp2_dgv1.Rows[i].Cells["我公司型号规格"].Value, string.Empty)+"'"))
                {
                    tp2_dgv1.Rows[i].Cells["状态"].Value = "我公司型号规格在系统中不存在，请检查后重试！";
                    continue;
                }
                //检查客户名称及物资代码是否在数据库中已存在
                for (int k=0;k<tp2_chklstCustomerNames.SelectedItems.Count;k++)
                {
                    string strCheckMaterial = "select ID from CustomerMaterialAdapt where MaterialID='" + GeneralTools.GetString(tp2_dgv1.Rows[i].Cells["物资代码"].Value, string.Empty) + "' and CustomerName = '" + tp2_chklstCustomerNames .SelectedItems [k].ToString() + "'"; 
                    if (sqt.RecordExist (strCheckMaterial ))
                    {
                        if (!GeneralTools.GetString(tp2_dgv1.Rows[i].Cells["状态"].Value, string.Empty).Equals(string.Empty))
                        {
                            tp2_dgv1.Rows[i].Cells["状态"].Value = GeneralTools.GetString(tp2_dgv1.Rows[i].Cells["状态"].Value, string.Empty) + "\n";
                        }
                        tp2_dgv1.Rows[i].Cells["状态"].Value = "物资编码在客户名称\"" + tp2_chklstCustomerNames.SelectedItems [k].ToString() + "\"中已存在！";
                    }
                }
            }
            //检查是否有出错记录
            bool bHasErr = false;
            for (int i = 0; i < tp2_dgv1.Rows.Count; i++)
            {
                if (tp2_dgv1.Rows[i].IsNewRow)
                {
                    continue;
                }
                if (!GeneralTools.GetString(tp2_dgv1.Rows[i].Cells["状态"].Value, string.Empty).Trim ().Equals(string.Empty))
                {
                    bHasErr = true;
                    tp2_dgv1.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;
                }
            }
            if (bHasErr )
            {
                return;
            }
            tp2_dgv1.AutoResizeRows();
            //保存设置
            string strInsertString = "insert into CustomerMaterialAdapt (CustomerName ,MaterialID ,CustomerModel,OwnModel ,Creater ,CreateTime) values('@客户名称','@物资代码','@客户型号规格','@我公司型号规格','@创建者',Getdate())";
            for (int i = 0; i < tp2_dgv1.Rows.Count; i++)
            {
                if (tp2_dgv1.Rows[i].IsNewRow)
                {
                    continue;
                }
                for (int k = 0; k < tp2_chklstCustomerNames.SelectedItems .Count; k++)
                {
                    sqt.DoCmdExecuteNonQuery(strInsertString.Replace("@客户名称",tp2_chklstCustomerNames .SelectedItems [k].ToString ())
                                                                                        .Replace("@物资代码", GeneralTools.GetString(tp2_dgv1.Rows[i].Cells["物资代码"].Value, string.Empty))
                                                                                        .Replace("@客户型号规格", GeneralTools.GetString(tp2_dgv1.Rows[i].Cells["客户型号规格"].Value, string.Empty))
                                                                                        .Replace("@我公司型号规格", GeneralTools.GetString(tp2_dgv1.Rows[i].Cells["我公司型号规格"].Value, string.Empty))
                                                                                        .Replace("@创建者", ui.UserName));
                }
            }
            tp2_dgv1.Rows.Clear();
            GeneralTools.ShowInfoMsg("设置保存完毕！");
        }

        private void tsmiSelectAll_Click(object sender, EventArgs e)
        {
            if (tp2_chklstCustomerNames .Items.Count >0)
            {
                for(int i=0;i<tp2_chklstCustomerNames.Items.Count;i++)
                {
                    tp2_chklstCustomerNames.SetSelected(i, true);
                }
            }
        }

        private void tp2_btnAutoSize_Click(object sender, EventArgs e)
        {
            tp2_dgv1.AutoResizeColumns();
        }
    }
}
